<?php
    echo '<div id="fone_addon'.$add.'" class="fonediv">';
    //Loại booking
    echo form_dropdown('booking_types[]', $this->voucher_model->getListBookingTypes(), '1','id="booking_types'.$add.'" class="num_cls"');
    echo form_error('booking_types');
    echo ' ';
    //hãng
    echo form_input(array('name' => 'airline[]', 'id' => 'airline'.$add, 'value' => (isset($voucher->airline))?$voucher->airline:'','required'=>'required','maxlength'=>'2','class'=>'num_cls','placeholder'=>$this->lang->line('airline')));
    echo form_error('airline');
    echo ' ';
    //code
    echo form_input(array('name' => 'booking_place_code[]', 'id' => 'booking_place_code'.$add, 'value' => (isset($voucher->booking_place_code))?$voucher->booking_place_code:'','required'=>'required','class'=>'num_cls','placeholder'=>$this->lang->line('booking_place_code')));
    echo form_error('booking_place_code');
    echo ' ';
    //ngay dat
    echo form_input(array('type' => 'hidden','name' => 'createdTime[]', 'id' => 'createdTime'.$add, 'value' => ''));
    //Giá net
    echo form_input(array('name' => 'net_fare[]', 'id' => 'net_fare'.$add, 'value' => '','readonly' => 'true','required'=>'required','class'=>'num_cls','placeholder'=>$this->lang->line('net_fare')));
    echo form_error('net_fare');
    echo form_input(array('type' => 'hidden','name' => 'net_fare_fake[]', 'id' => 'net_fare_fake'.$add, 'value' => '','class'=>'sumNet'));
    echo ' ';
    //Giá bán
    echo form_input(array('name' => 'sale_fare[]', 'id' => 'sale_fare'.$add, 'value' => '','readonly' => 'true','required'=>'required','class'=>'num_cls','placeholder'=>$this->lang->line('sale_fare')));
    echo form_error('sale_fare');
    echo form_input(array('type' => 'hidden','name' => 'sale_fare_fake[]', 'id' => 'sale_fare_fake'.$add, 'value' => '','class'=>'sumSale'));
    echo ' ';
    echo form_button(array('adata' => $add,'content' => $this->lang->line('delete'),'class' => 'btn btn-danger','onclick'=>'javascript:$(this).parent().remove();'));
    echo '</div>';
?>

<script type="text/javascript">
    $(document).ready(function() {
        var add = "<?php echo $add?>";
        $("#airline"+add).blur(function() {
                document.getElementById("airline"+add).value       = $("#airline"+add).val().toUpperCase().replace(/\s/g, '');
        });
            
        $("#booking_place_code"+add).blur(function() {
                var booking_types          = $("#booking_types"+add).val();
                var airline                = $("#airline"+add).val().toUpperCase().replace(/\s/g, '');
                var booking_place_code     = $("#booking_place_code"+add).val().toUpperCase().replace(/\s/g, '');
                var customerID             = $("#customerID").val().trim();
                if(airline.length != 2){
                    alert("Vui lòng nhập hãng hàng không gồm 2 chữ số!");$("#airline"+add).focus();$('button[type="submit"]').attr('disabled','disabled');return false;
                }

                if(booking_place_code == ""){
                    alert("<?php echo $this->lang->line('booking_place_code').' : '.$this->lang->line('require');?>");$("#booking_place_code"+add).focus();$('button[type="submit"]').attr('disabled','disabled');return false;
                }

                setTimeout(function () {
                    $.ajax(
                            {
                                url : '/auth/voucher/getInfoBookingHold',
                                type: "POST",
                                data : {booking_types:booking_types,airline:airline,booking_place_code:booking_place_code,customerID:customerID},
                                cache: true,
                                success:function(responseData) 
                                {
                                    var data    = JSON.parse(responseData);
                                        switch(data['error']){
                                            case 0:
                                                var data1    = JSON.parse(data['msg']);
                                                
                                                document.getElementById("net_fare"+add).value                   = FormatNumber(data1.net_fare);
                                                document.getElementById("net_fare_fake"+add).value              = data1.net_fare;
                                                document.getElementById("sale_fare"+add).value                  = FormatNumber(data1.sale_fare);
                                                document.getElementById("sale_fare_fake"+add).value             = data1.sale_fare;
                                                document.getElementById("createdTime"+add).value                = data1.createdTime;

                                                document.getElementById("airline"+add).setAttribute("readonly", "true");
                                                document.getElementById("booking_place_code"+add).setAttribute("readonly", "true");
                                                
                                                var sumNet = '';
                                                var sumSale = '';
                                                for(count = 1; count <= add; count++){
                                                    sumNet  = Number(sumNet) + Number($("#net_fare_fake"+count).val());
                                                    sumSale = Number(sumSale) + Number($("#sale_fare_fake"+count).val());
                                                }
                                                
                                                document.getElementById("sumNet").value                  = FormatNumber(sumNet);
                                                document.getElementById("sumNet_fake").value             = sumNet;
                                                document.getElementById("sumSale").value                 = FormatNumber(sumSale);
                                                document.getElementById("sumSale_fake").value            = sumSale;
                                                
                                                $("#booking_types"+add).attr("readonly","true");
                                                $('button[type="submit"]').removeAttr('disabled');
                                                break;
                                            case 1:
                                                document.getElementById("net_fare"+add).value                   = '';
                                                document.getElementById("net_fare_fake"+add).value              = '';
                                                document.getElementById("sale_fare"+add).value                  = '';
                                                document.getElementById("sale_fare_fake"+add).value             = '';
                                                
                                                alert("Không tìm thấy Code "+$("#booking_place_code"+add).val()+' - Hãng '+$("#airline"+add).val()+' đối với khách hàng này và booker này trên hệ thống booking hold');
                                                $("#airline"+add).focus();
                                                $('button[type="submit"]').attr('disabled','disabled');
                                                return false;
                                                break;
                                            case 2:
                                                document.getElementById("net_fare"+add).value                   = '';
                                                document.getElementById("net_fare_fake"+add).value              = '';
                                                document.getElementById("sale_fare"+add).value                  = '';
                                                document.getElementById("sale_fare_fake"+add).value             = '';
                                                
                                                var data1    = JSON.parse(data['msg']);
                                                alert("Code "+$("#booking_place_code"+add).val()+' - Hãng '+$("#airline"+add).val() + ' đã được tạo trong chứng từ số '+data1.id);
                                                $("#airline"+add).focus();
                                                $('button[type="submit"]').attr('disabled','disabled');
                                                return false;
                                                break;
                                        }
                                },
                                error: function() 
                                {

                                }
                            });
                }, 1000);
        });
        
        function FormatNumber(x) {
                if (typeof x === "undefined") {
                    return '';
                } else {
                return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + ' VNĐ';
                }
            };
            ///,|VNĐ|\s/g
            function UnFormatNumber(x) {
                if (typeof x === "undefined") {
                    return '';
                } else {
                return x.toString().replace(/,|VNĐ|\s/g, "");
                }
            };
    });
</script>